home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 24
/
Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso
/
Aminet
/
dev
/
c
/
cpost_1_4.lha
/
cpostp1.c
< prev
next >
Wrap
C/C++ Source or Header
|
1997-11-17
|
18KB
|
579 lines
/*------------------------------------------------------------------
* cpostp1.c : Pass 1 of cPost
*------------------------------------------------------------------
* 12-02-91 originally by Patrick J. Mueller
* 12-03-92 converted from cBook to cPost
*------------------------------------------------------------------*/
#ifdef AMIGA
#define USE_BUILTIN_MATH
#endif /* AMIGA */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ctok.h"
#include "cpost.h"
/*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
/*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
/*------------------------------------------------------------------
* add bracketing starts in front of braces
*------------------------------------------------------------------*/
static void AddLBracket(
File *file,
char *line,
char *mask,
int *maxMask,
Tok *tok
)
{
int lIndent;
int rIndent;
int place;
Tok *sib;
/*---------------------------------------------------------------
* see if matching bracket is on this line - if so, skip
*---------------------------------------------------------------*/
sib = tok->sib;
if (sib && (sib->tok.line == tok->tok.line))
return;
/*---------------------------------------------------------------
* find minimum indent of left and right brace
*---------------------------------------------------------------*/
lIndent = strspn(line," ");
if (!tok->sib)
place = lIndent;
else
{
rIndent = strspn(file->line[sib->tok.line-1]," ");
place = min(lIndent,rIndent);
}
/*---------------------------------------------------------------
* if no indentation on { or }, don't bracket!
*---------------------------------------------------------------*/
if (!place)
return;
/*---------------------------------------------------------------
* we'll bracket the column BEFORE this
*---------------------------------------------------------------*/
place--;
/*---------------------------------------------------------------
* set the mask, maxMask, and write bracket to line
*---------------------------------------------------------------*/
mask[place]++;
*maxMask = max(place,*maxMask);
if (' ' == line[place])
line[place] = '\x01';
}
/*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
/*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
/*------------------------------------------------------------------
* add bracketing ends
*------------------------------------------------------------------*/
static void AddRBracket(
File *file,
char *line,
char *mask,
int *maxMask,
Tok *tok
)
{
Tok *sib;
/*---------------------------------------------------------------
* see if matching bracket is on this line - if so, skip
*---------------------------------------------------------------*/
sib = tok->sib;
if (sib && (sib->tok.line == tok->tok.line))
return;
/*---------------------------------------------------------------
* safety valve
*---------------------------------------------------------------*/
if (-1 == *maxMask)
return;
/*---------------------------------------------------------
* add end bracket to line
*---------------------------------------------------------*/
if (' ' == line[*maxMask])
line[*maxMask] = '\x03';
/*---------------------------------------------------------
* reset mask and maxMask
*---------------------------------------------------------*/
mask[*maxMask]--;
if (!mask[*maxMask])
{
*maxMask -= 1;
while (-1 != *maxMask)
if (mask[*maxMask])
break;
else
*maxMask -= 1;
}
}
/*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
/*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
/*------------------------------------------------------------------
* add bracketing chars to line based on mask
*------------------------------------------------------------------*/
static char *AddMBrackets(
File *file,
char *line,
char *mask,
int maxMask
)
{
int i;
/*------------------------------------------------------------
* see if we need to make the line longer
*------------------------------------------------------------*/
if (maxMask + 1 >= (int) strlen(line))
{
char *newLine;
newLine = malloc(maxMask+3);
if (!newLine)
cPostError(1,"out of memory!!!");
if ('\n' == line[strlen(line)-1])
line[strlen(line)-1] = ' ';
memset(newLine,' ',maxMask+1);
newLine[maxMask+1] = '\n';
newLine[maxMask+2] = 0;
memcpy(newLine,line,strlen(line));
free(line);
line = newLine;
}
/*---------------------------------------------------------------
* get number of first non-blank column
*---------------------------------------------------------------*/
maxMask = min(maxMask,(int)strspn(line," "));
/*---------------------------------------------------------------
* for each non-zero entry in mask, write bracket
*---------------------------------------------------------------*/
for (i=0; i<=maxMask; i++)
{
if (mask[i])
if (' ' == line[i])
line[i] = '\02';
}
return line;
}
/*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
/*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
/*------------------------------------------------------------------
* strip trailing blanks off a line (that has a \n at the end!!)
*------------------------------------------------------------------*/
static void StripTrailingBlanks(
char *line
)
{
int slen;
slen = strlen(line);
if ('\n' != line[slen-1])
{
fprintf(stderr,"line found without carriage return!!\n");
return;
}
line[slen-1] = ' ';
while (*line && (' ' == line[slen-1]))
{
line[slen-1] = '\0';
slen--;
}
line[slen] = '\n';
}
/*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
/*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
/*------------------------------------------------------------------
* add function information to trees and lists
*------------------------------------------------------------------*/
static void AddFunctionPrototype(
Info *info,
File *file,
char *name
)
{
Function *func;
func = GetFunction(info,name);
if (!ListFind(file->funcProList,&func))
if (!ListAdd(file->funcProList,&func))
cPostError(1,"error adding function prototype to list");
}
/*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
/*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
/*------------------------------------------------------------------
* add function information to trees and lists
*------------------------------------------------------------------*/
static void AddFunctionDefinition(
Info *info,
File *file,
char *name,
unsigned long lineNo
)
{
Function *func;
func = GetFunction(info,name);
if (!ListFind(file->funcDefList,&func))
if (!ListAdd(file->funcDefList,&func))
cPostError(1,"error adding function definition to list");
func->fileName = file->name;
func->lineNo = lineNo;
}
/*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
/*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
/*------------------------------------------------------------------
* add function information to trees and lists
*------------------------------------------------------------------*/
static void AddFunctionUsage(
Info *info,
char *calleeName,
char *callerName
)
{
Function *caller;
Function *callee;
callee = GetFunction(info,calleeName);
caller = GetFunction(info